/*
 * @name: **列表
 * @content: 
 * @Author: joyce
 * @Date: 2020-03-15 14:26:04
 */
// 引入node上传模块
const express = require(`express`)
const router = express.Router()
var multer = require('multer');
var fs = require("fs")
let path = require("path");

// 配置存储路径 和 重命名
var storage = multer.diskStorage({
    // 图片上传到服务器以后 要放置的路径
    destination: path.join(__dirname, '../public/images/timyceimgs/'),


    // 图片重命名
    filename: function (req, file, cb) {

        console.log(file)
        let extname = file.mimetype.split('/')[1];
        let imgname = Date.now() + parseInt(Math.random() * 999) + parseInt(Math.random() * 2222);
        let keepname = imgname + '富文本图片' + '.' + extname
        cb(null, keepname);
    }
});

// 上传对象
var upload = multer({
    storage,
});
// 接收上传请求
router.post('/api/v1/uploadtimyceimg', upload.single('file'), async (req, res) => {
    var stat=fs.lstatSync('../public/images/timyceimgs/');
    console.log(stat.isDirectory(req.file.fieldname))
    if (stat.isDirectory(req.file.fieldname)) {
        res.jsonp({
            code: 20000,
            data: req.file.filename,
        });
    } else {
        res.jsonp({
            code: 40000
        });
    }
})
module.exports = router